$(window).load(function() {
    $('#slider').nivoSlider({
    	effect: 'fade',
    	captionOpacity: 1,
    	directionNav: false
    });
});

function swipeLeft(event)
{
	currentLeft= $('#home-bottom-slider > ul').css('left');
	length = $('#home-bottom-slider > ul > li').length /2
	if(currentLeft =='auto' ||!currentLeft){
		currentLeft = 0;
	}else{
		currentLeft = currentLeft.replace('px', '');
	}
	if(currentLeft > -(length - 6) * 160){
		$('#home-bottom-slider > ul').animate({ left: currentLeft - 160 }, 'slow');
	}
}
function swipeRight(event)
{
	currentLeft= $('#home-bottom-slider > ul').css('left');
	length = $('#home-bottom-slider > ul > li').length /2
	if(currentLeft =='auto' ||!currentLeft){
		currentLeft = 0;
	}else{
		currentLeft = currentLeft.replace('px', '');
	}
	
	if(currentLeft < 0){
		currentLeft = currentLeft.replace('-', '');
		left = 160 - currentLeft;
		$('#home-bottom-slider > ul').animate({ left: left }, 'slow');
	}
	
}

$(document).ready(function() {
	if( navigator.userAgent.match(/Android/i)
   		 || navigator.userAgent.match(/webOS/i)
   		 || navigator.userAgent.match(/iPhone/i)
   		 || navigator.userAgent.match(/iPad/i)
   		 || navigator.userAgent.match(/iPod/i)
   		 || navigator.userAgent.match(/BlackBerry/i)
   		 ){
   	//$('#home-bottom-slider > ul').swipeGallery({height: '190px', width: '150px'});
   	/*
   	$('#home-bottom-slider > ul').bind('swipe', function(){
   		diffWithPrevious = this.diff(-2, -1);
   		if (this.state === STATE.ENDED){
   			currentLeft= $('#home-bottom-slider > ul').css('left');
   			if(diffWithPrevious.x < 0){
   				$('#home-bottom-slider > ul').animate({ left: currentLeft - this.size }, 'slow');
   			}else{
   				$('#home-bottom-slider > ul').animate({ left: currentLeft + this.size }, 'slow');	
   			}
   		}
   	});
   	*/
 var myCarousel = $('#home-bottom-slider').scrollingCarousel({
		scrollSpeed: 'slow',
		looped: false
	});
   	var swipeOptions=
		{
			swipeLeft:swipeLeft,
			swipeRight:swipeRight,
			threshold:0
		}
   	/*
   	$('#home-bottom-scroll').css('overflow', 'hidden');
   	$('#home-bottom-scroll').css('position', 'relative');
   	$('#home-bottom-slider > ul').css('position', 'absolute');
   	$('#home-bottom-slider > ul').css('width', $('#home-bottom-slider > ul > li').length /2 * 160 + 'px');
   	*/
   	$('#home-bottom-slider > ul').swipe( swipeOptions );
   	//$('#home-bottom-slider > ul > li').swipe( swipeOptions );
   }else{
	   var myCarousel = $('#home-bottom-slider').scrollingCarousel({
			scrollSpeed: 'slow',
			looped: false
		});
   }
		
	$('#main-menu-header').hover(function(){
		$('#main-menu > ul').css('display', 'block');
	});
	$('#main-menu-header').mouseout(function(){
		$('#main-menu > ul').css('display', 'none');
	});
	$('#main-menu > ul').mouseover(function(){
		$('#main-menu > ul').css('display', 'block');
	});
	
	$('#main-menu > ul').mouseleave(function(){
		$('#main-menu > ul').css('display', 'none');
	});
	
	$('#month-slider').slider({
		min: 1,
		max: 12,
		value: 1,
		step: 1,
		stop: function( event, ui ) {
			value = ui.value;
			value =	parseInt(value, 10);
			if(value <10){
				value = '0' + value;
			}
			$('#month-wrapper a').removeClass('selected');
			$('#month-wrapper a#month_' + value).addClass('selected');
			$('#home-bottom-slider > ul > li').find("a[href^='#"+value+"']").first().click();
			
			liItem = $('#home-bottom-slider > ul > li').find("a[href^='#"+value+"']").first();			
			length = $('#home-bottom-slider > ul > li').length * 165/2 - 6;
			index = $('#home-bottom-slider > ul > li').index(liItem.parent().parent()) + 1;
			
			if(index){
				if(index < 4){
					//centerPos = 165*(4-index);
					centerPos = 0;
					$('#home-bottom-slider > ul').animate({ left: centerPos }, 'slow');
				//	$('#home-bottom-slider > ul').next().animate({ left: centerPos - length  }, 'slow');
				}else if(index <10){
					centerPos = -165*(index-3) + 80;
					$('#home-bottom-slider > ul').animate({ left: centerPos }, 'slow');
				//	$('#home-bottom-slider > ul').next().animate({ left: length + centerPos }, 'slow');
				}else{
					centerPos = -160*6 + 12;
					$('#home-bottom-slider > ul').animate({ left: centerPos }, 'slow');
				}
			}
			
		}
	});
	
	$('#home-bottom-slider a').click(function(){
		id = $(this).attr('id').replace('folder_', '');
		value = $(this).attr('href').replace('#', '');
		
		if(parseInt(value) == 0){
			value1 = value.replace('0', '');
		}else{
			value1 = value;
		}
		$("#month-slider").slider( "value", parseInt(value1));
		$('#month-wrapper a').removeClass('selected');
		$('#month-wrapper a#month_' + parseInt(value1)).addClass('selected');
		
		$('.slider-wrapper').html('<div id="loading-image"><img src="'+SITE_URL+'media/images/loading.gif" /></div>');
		$('#indicator-image > img').css('display', 'none');
		$('.slider-wrapper').load(SITE_URL+'forums/get_image_set/'+id, function(){
			if($('.slider-wrapper').find('img').length){
				$('#slider img').load(function(){
					$('#slider').nivoSlider({
						effect: 'fade',
				    	captionOpacity: 1,
				    	directionNav: false
				    });
					$('#indicator-image > img').css('display', 'block');
				});
			}
			
		});
		return false;
	});
	
	$('#month-wrapper a').click(function(){
		$('#month-wrapper a').removeClass('selected');
		$(this).addClass('selected');
		id = $(this).attr('id').replace('month_', '');
		value = id;
		value =	parseInt(value, 10);
		if(value <10){
			value = '0' + value;
		}
		$("#month-slider").slider( "value", id);
		$('#home-bottom-slider > ul > li').find("a[href^='#"+value+"']").first().click();
		liItem = $('#home-bottom-slider > ul > li').find("a[href^='#"+value+"']").first();			
		length = $('#home-bottom-slider > ul > li').length * 165/2 - 6;
		index = $('#home-bottom-slider > ul > li').index(liItem.parent().parent()) + 1;
		
		if(index){
			if(index < 4){
				//centerPos = 165*(4-index);
				centerPos = 0;
				$('#home-bottom-slider > ul').animate({ left: centerPos }, 'slow');
				//$('#home-bottom-slider > ul').next().animate({ left: centerPos - length  }, 'slow');
			}else if(index <10){
				centerPos = -165*(index-3) + 80;
				$('#home-bottom-slider > ul').animate({ left: centerPos }, 'slow');
				//$('#home-bottom-slider > ul').next().animate({ left: length + centerPos }, 'slow');
			}else{
				centerPos = -160*6 +12;
				$('#home-bottom-slider > ul').animate({ left: centerPos }, 'slow');
			}
		}
		return false;
	});
});